package com.example.app05_activity_demo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnLongClickListener {

    private static final String TAG = "MainActivity";

    private EditText etMainNumber;
    private EditText etMainContent;
    private Button btnMainCall;
    private Button btnMainSend;

    private final View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.btn_main_call) {
                // 打电话
                // 隐式调用拨号界面action
                Intent intent = new Intent(Intent.ACTION_DIAL);
                // 携带数据
                String number = etMainNumber.getText().toString();
                intent.setData(Uri.parse("tel:" + number));
                startActivity(intent);
            } else if (v.getId() == R.id.btn_main_send) {
                // 发短信
                // 隐式调用短信编辑界面action
                Intent intent = new Intent(Intent.ACTION_SENDTO);
                // 携带数据
                String number = etMainNumber.getText().toString();
                String message = etMainContent.getText().toString();
                intent.setData(Uri.parse("smsto:" + number));
                // 短信的内容
                // 短信内容的key一定是 sms_body
                intent.putExtra("sms_body", message);
                startActivity(intent);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, "----> onCreate()");

        // 初始化视图组件
        etMainNumber = findViewById(R.id.et_main_number);
        etMainContent = findViewById(R.id.et_main_content);
        btnMainCall = findViewById(R.id.btn_main_call);
        btnMainSend = findViewById(R.id.btn_main_send);

        // 给视图对象设置点击监听
        btnMainCall.setOnClickListener(onClickListener);
        btnMainSend.setOnClickListener(onClickListener);

        // 给视图对象设置长按监听
        btnMainCall.setOnLongClickListener(this);
        btnMainSend.setOnLongClickListener(this);
    }

    /**
     * 长按监听事件
     * @param v 发生事件的视图对象
     * @return bool
     */
    @Override
    public boolean onLongClick(View v) {
        if (v.getId() == R.id.btn_main_call) {
            // 长按打电话
            // 隐式打电话界面
            // 需要开启打电话权限
            Intent intent = new Intent(Intent.ACTION_CALL);
            // 携带数据
            String number = etMainNumber.getText().toString();
            intent.setData(Uri.parse("tel:" + number));
            // 启动Activity
            startActivity(intent);
        } else if (v.getId() == R.id.btn_main_send) {
            // 长按发短信
            // 获取SmsManager对象
            SmsManager smsManager = SmsManager.getDefault();
            // 发送短信
            String number = etMainNumber.getText().toString();
            String message = etMainContent.getText().toString();
            // 需要申请发短信的权限
            smsManager.sendTextMessage(number, null, message, null, null);
            Toast.makeText(this, "发送成功", Toast.LENGTH_LONG).show();
        }
        // 返回true表示不会再触发点击事件
        // 表示此事件已经被消费了
        return true;
    }
}